
#ifndef UTFCONVERTER_H__
#define UTFCONVERTER_H__


#include "ConvertUTF2.h"


typedef std::basic_string< UTF16 > utf16string;


static 
utf16string UTF8ToUTF16_2( const std::string& utf8 ) {
  ConversionResult ret;
  const UTF8* source_start = reinterpret_cast< const UTF8* >( utf8.c_str() );
  const UTF8* source_end   = source_start + utf8.length();
  UTF16* target_start = NULL;
  UTF16* target_end   = NULL;
  unsigned int bytes = 0;
 
  /* Test conversion and find size UTF* of buffer we need */
  ret = ConvertUTF8toUTF16_2((const UTF8 **)&source_start, source_end,
              NULL, NULL, strictConversion, &bytes);
    /*printf("Bytes Required = %d\n", bytes);*/
  if (ret != conversionOK) {
    if (ret == sourceExhausted) {
      printf("_dosvwc: Partial character in input");
    } else if (ret == targetExhausted) {
      printf("_dosvwc: target buffer exhausted");
    } else if (ret == sourceIllegal) {
      printf("_dosvwc: malformed/illegal source sequence");
    } else {
      printf("_dosvwc: unknown ConvertUTF16toUTF8 error");
    }
  }
    
  source_start = reinterpret_cast< const UTF8* >( utf8.c_str() );
  source_end   = source_start + utf8.length();
  utf16string utf16;
  unsigned int len = bytes/sizeof(UTF16);
  utf16.resize( len );
  target_start = reinterpret_cast< UTF16* >( &utf16[0] );
  target_end   = target_start + len;
  ret = ConvertUTF8toUTF16_2( &source_start, source_end, &target_start, target_end, strictConversion, &bytes );
  if( ret == conversionOK ) {
    return utf16;
  } else {
    return utf16string();
  }    
}

static 
std::string UTF16ToUTF8_2( const utf16string& utf16 ) {
  ConversionResult ret;
  UTF16 *source_start = (UTF16*)utf16.c_str();
  UTF16 *source_end = source_start + utf16.size();
  UTF8 *target_start;
  UTF8 *target_end;
  unsigned int bytes = 0;
  
  /* Test conversion and find size UTF* of buffer we need */
  ret = ConvertUTF16toUTF8_2((const UTF16 **)&source_start, source_end,
              NULL, NULL, strictConversion, &bytes);
    /*printf("Bytes Required = %d\n", bytes);*/
  if (ret != conversionOK) {
    if (ret == sourceExhausted) {
      printf("_dosvwc: Partial character in input");
    } else if (ret == targetExhausted) {
      printf("_dosvwc: target buffer exhausted");
    } else if (ret == sourceIllegal) {
      printf("_dosvwc: malformed/illegal source sequence");
    } else {
      printf("_dosvwc: unknown ConvertUTF16toUTF8 error");
    }
  }

  source_start = (UTF16*)utf16.c_str();
  source_end = source_start + utf16.size();
  std::string utf8string;
  utf8string.resize( bytes );
  /* convert UTF16 to UTF8 */
  target_start = (UTF8*)utf8string.c_str();
  target_end = target_start + bytes + 1;
  ret = ConvertUTF16toUTF8_2((const UTF16 **)&source_start, source_end, &target_start, target_end, strictConversion, &bytes);
  /*fprintf(stderr, "%s\n", p);*/

  //if (ret != conversionOK) {
  //  printf("_dosvwc: second call to ConvertUTF16toUTF8 failed (%d)", ret);
  //}
    
    return utf8string;
}


#endif // UTFCONVERTER_H__
